package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.domain.Category;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.CategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;

    @Autowired
    private CategoryService categoryService;

    @Override
    public List<CategoryBrand> getPageList(CategoryBrand categoryBrand) {
        return categoryBrandMapper.getPageList(categoryBrand);
    }

    @Override
    public boolean saveCategoryBrand(CategoryBrand categoryBrand) {
        //根据分类ID和品牌ID来查询该品牌是否存在
        LambdaQueryWrapper<CategoryBrand> queryWrapper=new LambdaQueryWrapper<>();
        Long count = baseMapper.selectCount(queryWrapper.eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId()).eq(CategoryBrand::getBrandId,categoryBrand.getBrandId()));
        if (count>0){
            //说明已经存在该分类
            throw new ServiceException("数据库中已经添加过该分类品牌");
        }
        return baseMapper.insert(categoryBrand)>0;
    }

    @Override
    public CategoryBrand getCategoryBrandById(Long id) {
        //当前分类的对象,赋值了一个id
        CategoryBrand categoryBrand = baseMapper.selectById(id);
        //创建一个集合存放父id
        List<Long> parentIds = new ArrayList<>();
        //根据当前分类对象中id查询的categoryId
        Long categoryId = categoryBrand.getCategoryId();

        //获取当前分类id的父id
        List<Long> categoryIdList= getParentIds(categoryId,parentIds);
        //将集合中的元素反转
        Collections.reverse(categoryIdList);
        //把当前分类的父id设置到集合中
        categoryIdList.add(categoryId);
        //给当前分类品牌蛇者分类id和父id
        categoryBrand.setCategoryIdList(categoryIdList);
        return categoryBrand;
    }

    @Override
    public int updateCategoryBrandById(CategoryBrand categoryBrand) {
        //根据id查询品牌信息
        CategoryBrand categoryBrandDatebase = baseMapper.selectById(categoryBrand.getId());
        //判断用户有没有修数据库中的分类品牌数据
        if (categoryBrand.getBrandId().equals(categoryBrandDatebase.getBrandId())
                && categoryBrand.getCategoryId().equals(categoryBrandDatebase.getCategoryId())){
            return 1;
        }
        //判断当前修改之后的分类品牌数据库是否存在
        //根据分类id和品牌id查询数据库
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryBrand::getCategoryId,categoryBrand.getCategoryId())
                .eq(CategoryBrand::getBrandId,categoryBrand.getBrandId());
        CategoryBrand categoryBrand1 = baseMapper.selectOne(queryWrapper);
        if (categoryBrand1!=null){
            throw new ServiceException("该分类品牌在数据库中已经存在");
        }
        int updateById = baseMapper.updateById(categoryBrand);
        return updateById;
    }

    @Override
    public List<Brand> getBrandListBycategoryId(Long categoryId) {

        return baseMapper.getBrandListBycategoryId(categoryId);
    }

    public List<Long> getParentIds(Long categoryId, List<Long> parentIds) {
        Category category = categoryService.getById(categoryId);
        //获取当前分类的父id
        Long parentId = category.getParentId();
        if (parentId>0){
            parentIds.add(parentId);
            return getParentIds(parentId,parentIds);
        }
        return parentIds;
    }


}
